//
//  CETabbarController.m
//  C8 Energy
//
//  Created by 颜魏 on 2024/10/1.
//

#import "CETabbarController.h"
#import "CEHomeViewController.h"
#import "CESettingsViewController.h"

@interface CETabbarController ()

@end

@implementation CETabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.tintColor = [UIColor whiteColor];
    self.tabBar.barTintColor = [UIColor whiteColor];
    
    [self loadViewControllers];
}

- (void)loadViewControllers {
    CEHomeViewController *homeVC = [[CEHomeViewController alloc] initWithNibName:@"CEHomeViewController" bundle:nil];

    CESettingsViewController *settingVC = [[CESettingsViewController alloc] initWithNibName:@"CESettingsViewController" bundle:nil];

    [self setTabBarItemWithController:homeVC title:NSLocalizedString(@"Real time status", nil) imageName:@"tab_1_n" selectedImageName:@"tab_1_s"];
    [self setTabBarItemWithController:settingVC title:NSLocalizedString(@"Parameter setting", nil) imageName:@"tab_2_n" selectedImageName:@"tab_2_s"];

    self.viewControllers = @[homeVC, settingVC];
}

- (void)setTabBarItemWithController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    UITabBarItem *item = [[UITabBarItem alloc]init];
    item.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.title = title;
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB16(0x3a3a3a)} forState:UIControlStateSelected];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB16(0x9a9a9a)} forState:UIControlStateNormal];

    vc.tabBarItem = item;
    
    // 解决出现系统蓝问题
    if(@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [UITabBarAppearance new];
        // 设置未被选中的颜色
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: RGB16(0x9a9a9a)};
        // 设置被选中时的颜色
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: RGB16(0x3a3a3a)};
        [UITabBar appearance].standardAppearance = appearance;
    }
}


@end
